﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Catering_DBOpertion;

namespace Catering_Operation
{
    public partial class StatusReportInfo
    {
        public StatusReportInfo() { }

        /// <summary>
        /// 添加状态报告表信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool AddStatusReportInfo(Catering_Model.StatusReportInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("INSERT INTO StatusReport(");
            strSql.Append("MsgReference,Receiver,OriginSendTime,ArrivedTime,CommPort)");
            strSql.Append(" VALUES (");
            strSql.Append("@MsgReference,@Receiver,@OriginSendTime,@ArrivedTime,@CommPort)");
            SqlParameter[] parameters = {
                    new SqlParameter("@MsgReference", SqlDbType.Int,4),
					new SqlParameter("@Receiver", SqlDbType.NVarChar,50),
					new SqlParameter("@OriginSendTime", SqlDbType.DateTime,18),
					new SqlParameter("@ArrivedTime", SqlDbType.DateTime,18),
					new SqlParameter("@CommPort", SqlDbType.Int,4)};
            parameters[0].Value = model.MsgReference;
            parameters[1].Value = model.Receiver;
            parameters[2].Value = model.OriginSendTime;
            parameters[3].Value = model.ArrivedTime;
            parameters[4].Value = model.CommPort;
            return Convert.ToBoolean(DbHelperSQL.ExecuteSql(strSql.ToString(), parameters));
        }

        /// <summary>
        /// 删除一条状态报告表信息
        /// </summary>
        public bool DeleteStatusReportInfo(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("DELETE FROM StatusReport ");
            strSql.Append(" WHERE ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)};
            parameters[0].Value = ID;
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 批量删除状态报告表信息数据
        /// </summary>
        /// <param name="whereClause">删除条件</param>
        /// <returns></returns>
        public bool DeleteStatusReportInfoList(string whereClause)
        {
            string sql = "DELETE FROM StatusReport WHERE 1=1 " + whereClause;
            int rows = DbHelperSQL.ExecuteSql(sql);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到一个状态报告表信息对象实体
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public Catering_Model.StatusReportInfo GetStatusReportInfoModel(int ID)
        {
            string sql = "SELECT TOP 1 * FROM StatusReport WHERE ID=@ID ";
            SqlParameter[] parameters = { new SqlParameter("@ID", SqlDbType.Int, 4) };
            parameters[0].Value = ID;
            Catering_Model.StatusReportInfo model = new Catering_Model.StatusReportInfo();
            DataSet ds = DbHelperSQL.Query(sql, parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (!(ds.Tables[0].Rows[0]["ID"] is DBNull) && ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                if (!(ds.Tables[0].Rows[0]["MsgReference"] is DBNull) && ds.Tables[0].Rows[0]["MsgReference"].ToString() != "")
                {
                    model.MsgReference = int.Parse(ds.Tables[0].Rows[0]["MsgReference"].ToString());
                }
                if (!(ds.Tables[0].Rows[0]["Receiver"] is DBNull) && ds.Tables[0].Rows[0]["Receiver"].ToString() != "")
                {
                    model.Receiver = ds.Tables[0].Rows[0]["Receiver"].ToString();
                }
                if (!(ds.Tables[0].Rows[0]["OriginSendTime"] is DBNull) && ds.Tables[0].Rows[0]["OriginSendTime"].ToString() != "")
                {
                    model.OriginSendTime = DateTime.Parse(ds.Tables[0].Rows[0]["OriginSendTime"].ToString());
                }
                if (!(ds.Tables[0].Rows[0]["ArrivedTime"] is DBNull) && ds.Tables[0].Rows[0]["ArrivedTime"].ToString() != "")
                {
                    model.ArrivedTime = DateTime.Parse(ds.Tables[0].Rows[0]["ArrivedTime"].ToString());
                }
                if (!(ds.Tables[0].Rows[0]["CommPort"] is DBNull) && ds.Tables[0].Rows[0]["CommPort"].ToString() != "")
                {
                    model.CommPort = int.Parse(ds.Tables[0].Rows[0]["CommPort"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得状态报告表信息数据列表
        /// </summary>
        public DataSet GetStatusReportInfoList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM StatusReport ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" WHERE 1=1 " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得状态报告表信息数据列表数量
        /// </summary>
        public int GetStatusReportInfoListCount(string strWhere)
        {
            string strSql = "SELECT COUNT(*) FROM StatusReport WHERE 1=1 " + strWhere;
            return Convert.ToInt32(DbHelperSQL.GetSingle(strSql));
        }
    }
}
